package com.tanhua.dubbo.api;


import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private FriendApi friendApi;

    /**
     * 右滑 喜欢
     *
     * @param userId
     * @param loginUserId
     * @return
     */
    @Override
    public Boolean loveUser(Long userId, Long loginUserId) {
        long timeMillis = System.currentTimeMillis();
        Query query = new Query(Criteria.where("userId").is(loginUserId).and("likeUserId").is(userId));
        if (mongoTemplate.exists(query, UserLike.class)) {
            Update update = new Update();
            update.set("isLike", true);
            update.set("updated", timeMillis);
            mongoTemplate.updateFirst(query, update, UserLike.class);
        } else {
            UserLike userLike = new UserLike();
            userLike.setLikeUserId(userId);
            userLike.setUserId(loginUserId);
            userLike.setIsLike(true);
            userLike.setCreated(timeMillis);
            userLike.setUpdated(timeMillis);
            mongoTemplate.insert(userLike);
        }
        query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(loginUserId).and("isLike").is(true));
        if (mongoTemplate.exists(query, UserLike.class)) {
            friendApi.addContacts(userId, loginUserId);
            return true;
        }
        return false;
    }

    /**
     * 左滑 不喜欢
     *
     * @param id
     * @param LoginUserId
     */
    @Override
    public void notLikeUser(Long id, Long LoginUserId) {
        long timeMillis = System.currentTimeMillis();
        Query query = new Query(Criteria.where("userId").is(LoginUserId).and("likeUserId").is(id).and("isLike").is(true));
        if (mongoTemplate.exists(query, UserLike.class)) {
            Update update = new Update();
            update.set("isLike", false);
            update.set("updated", timeMillis);
            mongoTemplate.updateFirst(query, update, UserLike.class);
        } else {
            UserLike userLike = new UserLike();
            userLike.setLikeUserId(id);
            userLike.setUserId(LoginUserId);
            userLike.setIsLike(false);
            userLike.setCreated(timeMillis);
            userLike.setUpdated(timeMillis);
            mongoTemplate.insert(userLike);
        }

    }

    /**
     * 获取被喜欢列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<UserLike> getLoves(Long page, Long pagesize, Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        //判断数量是否超过10条
        long counts = mongoTemplate.count(query, UserLike.class);
        if (counts > 10) {
            pagesize = 10L;
            query.skip((page - 1) * pagesize).limit(pagesize.intValue());
        }
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        query = new Query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageResult<>(page, pagesize, count, userLikes);
    }

    /**
     * 获取喜欢列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult getMyLikeUsers(Long page, Long pagesize, Long userId) {
        //构建查询条件
        Query query = new Query(Criteria.where("userId").is(userId).and("isLike").is(true));
        //判断数量是否超过10条
        long counts = mongoTemplate.count(query, UserLike.class);
        if (counts > 10) {
            pagesize = 10L;
            query.skip((page - 1) * pagesize).limit(pagesize.intValue());
        }
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        query = new Query(Criteria.where("userId").is(userId).and("isLike").is(true));
        long count = mongoTemplate.count(query, UserLike.class);

        return new PageResult(page, pagesize, count, userLikeList);
    }
    /**
     *  获取互相喜欢，喜欢，粉丝数
     * @return
     */
    @Override
    public CountsVo getCounts(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        int likeTo = 0;
        for (UserLike userLike : userLikeList) {
            query = new Query(Criteria.where("userId").is(userLike.getLikeUserId()).and("likeUserId").is(userId).and("isLike").is(true));
            if (mongoTemplate.exists(query, UserLike.class)) {
                likeTo++;
            }

        }
        query = new Query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        long count = mongoTemplate.count(query, UserLike.class);
        return new CountsVo((long) likeTo, (long) userLikeList.size(),count);
    }


}
